package tests;

public class MyFirstThreadTest {
    private static int x, count;

    public synchronized static int getX() {
        return x;
    }

    public synchronized static void setX(int x) {
        MyFirstThreadTest.x = x;
    }

    public synchronized static int getCount() {
        return count;
    }

    public synchronized static void setCount() {
        count ++;
    }

    private static class Thread1 implements Runnable {

        @Override
        public void run() {
            setCount();
            setX(getX() + 100);
            System.out.println("Thread1 - x=" + x + " - count=" + getCount());
        }
    }

    private static class Thread2 implements Runnable {
        @Override
        public void run() {
            setCount();
            setX(getX() + 1);
            System.out.println("Thread2 - x=" + x + " - count=" + getCount());
        }
    }

    public static void main(String[] args) {

        Thread t1 = new Thread(new Thread1());
        Thread t2 = new Thread(new Thread2());

        t1.start();
        t2.start();

        while(t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("x=" + x);
    }
}
